package com.sugar.queryinfo.module.business.service.dataquerynomraltype;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.queryinfo.module.business.controller.admin.dataquerynomraltype.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.dataquerynomraltype.DataQueryNomralTypeDO;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;

import com.sugar.queryinfo.module.business.dal.mysql.dataquerynomraltype.DataQueryNomralTypeMapper;

import static com.sugar.queryinfo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.queryinfo.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.queryinfo.module.business.enums.ErrorCodeConstants.*;

/**
 * 数据查询默认类型 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class DataQueryNomralTypeServiceImpl implements DataQueryNomralTypeService {

    @Resource
    private DataQueryNomralTypeMapper dataQueryNomralTypeMapper;

    @Override
    public Long createDataQueryNomralType(DataQueryNomralTypeSaveReqVO createReqVO, Long userId) {
        DataQueryNomralTypeDO dataQueryNomralTypeDO = dataQueryNomralTypeMapper.selectOne(DataQueryNomralTypeDO::getUserId, userId);
        if (dataQueryNomralTypeDO == null){
            // 插入
            dataQueryNomralTypeDO = BeanUtils.toBean(createReqVO, DataQueryNomralTypeDO.class);
            dataQueryNomralTypeDO.setUserId(userId);
            dataQueryNomralTypeMapper.insert(dataQueryNomralTypeDO);

            // 返回
            return dataQueryNomralTypeDO.getId();
        }else {
            // 更新
            DataQueryNomralTypeDO updateObj = BeanUtils.toBean(createReqVO, DataQueryNomralTypeDO.class);
            updateObj.setId(dataQueryNomralTypeDO.getId());
            dataQueryNomralTypeMapper.updateById(updateObj);
            return dataQueryNomralTypeDO.getId();
        }
    }

    @Override
    public DataQueryNomralTypeDO getDataQueryNomralType(Long userId) {
        return dataQueryNomralTypeMapper.selectOne(DataQueryNomralTypeDO::getUserId, userId);
    }
}